import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

// 控制器 + 导航栏透明
class LxBaseScrollOpacityController extends GetxController {
  // 滑动监听
  ScrollController scrollController = ScrollController();

  // 顶部bar的透明度，默认为透明，1为不透明
  RxDouble appBarOpacity = 0.0.obs;

  // y轴 偏移距离阈值【显示/隐藏导航栏】
  // ignore: non_constant_identifier_names
  double showNavigationByOffset_y = 100.h;

  // y轴 偏移距离
  // ignore: non_constant_identifier_names
  double scrollOffset_y = 0.0;

  @override
  void onInit() {
    super.onInit();

    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
    // 监听
    scrollController.addListener(() {
      // 如果滑动的偏移量超出了自己设定的值，tab栏就进行透明化操作
      double alpha = scrollController.offset / showNavigationByOffset_y;
      scrollOffset_y = scrollController.position.pixels;
      if (alpha < 0.0) {
        alpha = 0.0;
      } else if (alpha > 1.0) {
        alpha = 1.0;
      }
      appBarOpacity.value = alpha;
    });
  }
}
